<?php
/**
 * 商品品牌
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class GoodsBrands extends Model
{
    //set talbename
    protected $table = 'goods_brands';
    //set primary key
    protected $pk = 'brand_id';

    /**
     * @desc 增加商品品牌
     * @param $goods_name
     * @param $logo
     * @param $display_order
     * @return false|int
     */
    public function addGoodsBrand($band_name, $logo, $is_show, $display_order)
    {
        $param['brand_name'] = $band_name;
        $param['logo'] = $logo;
        $param['is_show'] = $is_show;
        $param['display_order'] = $display_order;
        $param['create_time'] = time();
        $param['update_time'] = time();
        return $this->save($param);
    }

    /**
     * @desc 编辑商品品牌
     * @param $param
     * @return false|int
     */
    public function modGoodsBrands($param)
    {
        $conds = array(
            'brand_id' => $param['brand_id'],
        );
        $field = array();
        foreach ($param as $key => $value) {
            if ($key == 'brand_id') {
                continue;
            }
            $field[$key] = $value;
        }
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @desc 获取品牌
     * @param $brand_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsBrands($brand_id)
    {
        $conds = array(
            'brand_id' => $brand_id,
        );
        $field = '*';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    public function getGoodsBrandList()
    {
        return $this->paginate(10);
    }

    /**
     * 后台管理需要品牌的id和name
     */
    public function getBrandNameList()
    {
        $fields = ['brand_id', 'brand_name'];
        return $this->field($fields)->select();
    }

    /**
     * @param $brand_id
     * @return int
     * @throws \think\Exception
     * 删除品牌
     */
    public function deleteBrand($brand_id)
    {
        $conds = [
            'brand_id' => $brand_id
        ];
        return $this->where($conds)->delete();

    }
}
